{% extends 'base/base.html' %}

{% block title %}用户管理 - 多用户报警系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h3><i class="fa fa-users" aria-hidden="true"></i> 用户管理</h3>
                    <a href="{% url 'accounts:user_create' %}" class="btn btn-primary">
                        <i class="fa fa-plus" aria-hidden="true"></i> 创建用户
                    </a>
                </div>
            </div>
            <div class="card-body">
                {% if users %}    
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>手机号</th>
                                    <th>角色</th>
                                    <th>加入时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for user in users %}
                                    <tr>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <i class="fa fa-user-circle-o" aria-hidden="true" style="margin-right: 8px;"></i>
                                                {{ user.username }}
                                                {% if user == request.user %}
                                                    <span class="badge bg-info ms-2">当前用户</span>
                                                {% endif %}
                                            </div>
                                        </td>
                                        <td>{{ user.email|default:'-' }}</td>
                                        <td>{{ user.phone|default:'-' }}</td>
                                        <td>
                                            <span class="badge {% if user.is_super_admin %}bg-primary{% else %}bg-secondary{% endif %}">
                                                {{ user.get_role_display }}
                                            </span>
                                        </td>
                                        <td>{{ user.date_joined|date:'Y年m月d日 H:i:s' }}</td>
                                        <td>
                                            <div class="btn-group">
                                                <a href="{% url 'accounts:user_edit' user.id %}" class="btn btn-primary btn-sm">
                                                    <i class="fa fa-pencil" aria-hidden="true"></i> 编辑
                                                </a>
                                                {% if user.id != request.user.id %}
                                                    <a href="{% url 'accounts:user_delete' user.id %}" class="btn btn-danger btn-sm" onclick="return confirm('确定要删除用户 {{ user.username }} 吗？');">
                                                        <i class="fa fa-trash" aria-hidden="true"></i> 删除
                                                    </a>
                                                {% else %}
                                                    <button class="btn btn-danger btn-sm" disabled>
                                                        <i class="fa fa-trash" aria-hidden="true"></i> 删除
                                                    </button>
                                                {% endif %}
                                            </div>
                                        </td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                {% else %}
                    <div class="text-center py-10">
                        <i class="fa fa-users" aria-hidden="true" style="font-size: 3rem; color: #ccc;"></i>
                        <p class="mt-3 text-muted">暂无用户</p>
                        <a href="{% url 'accounts:user_create' %}" class="btn btn-primary mt-3">
                            <i class="fa fa-plus" aria-hidden="true"></i> 创建第一个用户
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block javascript %}
<script>
    // 添加页面加载动画效果
    document.addEventListener('DOMContentLoaded', function() {
        // 为表格行添加悬停效果
        const tableRows = document.querySelectorAll('.table tbody tr');
        tableRows.forEach(row => {
            row.addEventListener('mouseenter', function() {
                this.classList.add('bg-light');
            });
            row.addEventListener('mouseleave', function() {
                this.classList.remove('bg-light');
            });
        });
    });
</script>
{% endblock %}